/**
 * Created by yjp on 2018/5/3.
 */

var $,element,layer,table,laydate;
var size = 10;
window.pageIndex = 1;
var filterParam = {
    'token': sessionStorage.getItem('userToken'),
    'size': size,
    'page': window.pageIndex
}

layui.use(['element','jquery','laydate','layer','table'], function () {
    $ = layui.jquery;  //jquery
    element = layui.element;  //元素操作
    layer = layui.layer;  //弹层
    table = layui.table;  //表格
    laydate = layui.laydate;  //日期
    form = layui.form;
    $form = $('form');
    
    firstInfo();  //访问加载页面
    

    //上架
    $('body').on('click', '.upGood', function () {
        var delParam = {
            'token': sessionStorage.getItem('userToken'),
            'is_publish': 1,
            'id': $(this).attr('gid')
        };
        offGoods(delParam);
    });
    //下架
    $('body').on('click', '.downGood', function () {
        var delParam = {
            'token': sessionStorage.getItem('userToken'),
            'is_publish': 0,
            'id': $(this).attr('gid')
        };
        offGoods(delParam);
    });

    
    //商品录入
    $('body').on('click', '.addMeterial', function () {
        var perContent = layer.open({
            type: 2,
            title: '商品录入',
            shadeClose: false,
            shade: 0.4,
            area: ['100%', '80%'],
            content: 'addGood.html'
        });
        layer.full(perContent);
    });
    //编辑商品
    $('body').on('click', '.edit', function () {
        var hiddenText = $(this).prev();
        sessionStorage.setItem('filter',hiddenText.attr('v'));
        var perContent = layer.open({
            type: 2,
            title: '编辑商品',
            shadeClose: true,
            shade: 0.4,
            area: ['100%', '100%'],
            content: 'addGood.html?gid='+ $(this).attr('gid')
        });
        layer.full(perContent);
    });

    //查看具体规格商品
    $('body').on('click', '.subGoods', function () {
        var perContent = layer.open({
            type: 2,
            title: '具体规格商品',
            shadeClose: false,
            shade: 0.4,
            area: ['100%', '100%'],
            content: 'subGoodList.html?bid='+ $(this).attr('bid')
        });
        layer.full(perContent);
    });
    
});


/**
 * 获取商品列表
 * param  token,page,size
 * url  /shop_goods/listManagement
 */
function listManagement(param, isInside) {
    $.ajax({
        url: top.server_url + "/shop_goods/listManagement",
        type: "post",
        dataType: "json",
        data: param,
        success: function (data) {
            if(data.status == 200) {
                $('#goodsInfo tbody').empty();
                if(data.payload.total)
                    window.allGoodAmount = data.payload.total;
                else
                    window.allGoodAmount = 0;

                if(!isInside) setPage();

                if(data.payload.data) {
                    var goodList = data.payload.data;
                    $.each(goodList, function (index,obj) {
                        var html = '';
                        html += '<tr gid="'+ obj.id +'">'
                        // html += '<td>'
                        // html += '<input type="checkbox"/>'
                        // html += '<label for="checkAll1">'
                        // html += '<div class="cbClick layui-unselect layui-form-checkbox" lay-skin="primary">'
                        // html += '<i class="layui-icon"></i>'
                        // html += '</div>'
                        // html += '</label>'
                        // html += '</td>'
                        html += '<td><img src="'+ picUrlTrans(obj.pictUrl, top.img_url) +'"/></td>'
                        html += '<td>'+ obj.title +'</td>'
                        html += '<td>'+ obj.totalNum +'</td>'
                        html += '<td>'+ obj.zkPrice +'</td>'
                        if(obj.is_publish == 0) {
                            html += '<td style="color: #FF5722">仅保存</td>'
                        }
                        else if(obj.is_publish == 1) {
                            html += '<td style="color:#009688;">立即生效</td>'
                        }
                        html += '<td>'+ obj.shopTitle +'</td>'
                        html += '<td>'+ obj.tkRate +'</td>'

                        // if(obj.long_picurl)  {
                        //     html += '<td><img src="'+ picUrlTrans(obj.long_picurl, top.img_url) +'"/></td>'
                        // } else {
                        //     html += '<td>--</td>'
                        // }

                        // if(obj.mp4) {
                        //     html += '<td>'+ obj.mp4 +'</td>'
                        // } else {
                        //     html += '<td>--</td>'
                        // }

                        html += '<td>'+ obj.volume +'</td>'

                        // html += '<td>'
                        // if(obj.pics && JSON.parse(obj.pics).length>0) {
                        //     $.each(JSON.parse(obj.pics), function (index,obj) {
                        //         html += '<img src="'+ picUrlTrans(obj, top.img_url) +'">'
                        //     });
                        // } else {
                        //     html += '--'
                        // }
                        // html += '</td>'

                        // html += '<td>'
                        // if(obj.detail) {
                        //     $.each(JSON.parse(obj.detail).pics, function (index,obj) {
                        //         html += '<img src="'+ picUrlTrans(obj, top.img_url) +'">'
                        //     });
                        // } else {
                        //     html += '--'
                        // }
                        // html += '</td>'

                        html += '<td>'+ (obj.sku ? obj.sku : '--') +'</td>'
                        html += '<td>'+ (obj.sku_value ? obj.sku_value : '--') +'</td>'
                        html += '<td>'
                        html += '<div class="layui-table-cell laytable-cell-1-10 padding0">'

                        html += '<input type="hidden" '
                        html += 'v="'+ escapeHtml(JSON.stringify(obj)) +'"'
                        html += '/>'
                        html += '<a class="layui-btn layui-btn-xs edit" gid="'+ obj.id +'">编辑</a>'
                        if(obj.is_publish==1) {
                            html += '<a class="layui-btn layui-btn-xs layui-btn-danger downGood" gid="'+ obj.id +'">下架</a>'
                        } else {
                            html += '<a class="layui-btn layui-btn-xs layui-btn-normal upGood" gid="'+ obj.id +'">上架</a>'
                        }
                        if(obj.sku && obj.sku!='{}') {
                            html += '<a class="layui-btn layui-btn-xs layui-btn-normal subGoods" bid="'+ obj.belong_id +'">具体规格商品</a>'
                        }
                        html += '</div>'
                        html += '</td>'
                        html += '</tr>'
                        $('#goodsInfo tbody').append(html);
                    });
                } else {
                    var noData = '<tr><td style="text-align: center;line-height: 80px" colspan="12">暂无数据！</td></tr>';
                    $('#goodsInfo tbody').append(noData);
                }
            }
            else if(data.status == 403) {
                top.location.href = "login.html";
            }
            else {
                layer.msg(data.err)
            }
        },
        error: function() {
            layer.alert("网络错误，请联系管理员或稍后重试！", {icon: 7});
        }
    });
}

/**
 * 上下架商品
 * param  token,id
 * url  /shop_goods/offGoods
 */
function offGoods(param) {
    $.ajax({
        url: top.server_url + "/shop_goods/offGoods",
        type: "post",
        dataType: "json",
        data: param,
        success: function (data) {
            if (data.status == 200) {
                firstInfo(true);  //删除成功刷新列表页
            }
            else {
                layer.msg(data.err)
            }
        },
        error: function() {
            layer.alert("网络错误，请联系管理员或稍后重试！", {icon: 7});
        }
    });
}


//首次加载调用的函数.
function firstInfo(isInside) {
    var goodParam = {
        'token': filterParam.token,
        'size': filterParam.size,
        'page': window.pageIndex
    }
    listManagement(goodParam, isInside);
}


//商品分页
function setPage() {
    $("#Pagination").pagination(window.allGoodAmount, {
        current_page:0,
        link_to:"#",
        num_edge_entries: 1, //边缘页数
        num_display_entries:4, //主体页数
        callback: pageselectCallback,
        ellipse_text: '....',
        items_per_page: size, //每页显示个数
        prev_text: "<上一页",
        next_text: "下一页>"
    });
    function pageselectCallback(page_index, jq) {
        if(window.pageIndex != page_index + 1){
            window.pageIndex = page_index + 1;
            firstInfo(true);
        }
    }
}


